{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "de765f16-341b-4dc3-b59d-b16437c7e050",
   "metadata": {},
   "source": [
    "\n",
    "## Structurizr DSL Code Generation\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e25cdbdc-daee-4a9b-b363-9080dc216927",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: langchain\n",
      "Version: 0.1.9\n",
      "Summary: Building applications with LLMs through composability\n",
      "Home-page: https://github.com/langchain-ai/langchain\n",
      "Author: \n",
      "Author-email: \n",
      "License: MIT\n",
      "Location: /opt/conda/lib/python3.11/site-packages\n",
      "Requires: aiohttp, dataclasses-json, jsonpatch, langchain-community, langchain-core, langsmith, numpy, pydantic, PyYAML, requests, SQLAlchemy, tenacity\n",
      "Required-by: \n"
     ]
    }
   ],
   "source": [
    "!pip show langchain"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e235425-1ced-4f4b-8ede-3c6b82a01e8f",
   "metadata": {},
   "source": [
    "## Modelfile\n",
    "\n",
    "基于Ollama创建Gemma定制化AI模型\n",
    "\n",
    "https://www.tnblog.net/hb/article/details/8333\n",
    "\n",
    "```bash\n",
    "docker exec -it ollama ollama create mistral:structurizr -f /root/.ollama/Modelfile\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e07b2c7d-62d7-457c-bbcb-298b29aa522c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Please define the following env variables in the .env file \n",
    "# \n",
    "# ollama_url = http://ollama:11434\n",
    "# model_name = mistral:structurizr\n",
    "# embedding_model=nomic-embed-text\n",
    "# num_thread=8\n",
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fa2048ea-dbc4-49d7-b855-8b546dce2edb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Use the utility.py to get llm, embedding and output parser\n",
    "import utility\n",
    "embeddings = utility.get_embeddings()\n",
    "llm = utility.get_llm()\n",
    "output_parser = utility.get_output_parser()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "533f69c6-d35b-44bf-8197-096d96aead37",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = llm | output_parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "80e44aba-49ad-4e70-a511-3b8daf0bb010",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The provided keyword is `workspace`. Here's the grammar for it based on the given rules and format:\n",
      "\n",
      "```\n",
      "workspace [name] [description] {\n",
      "    [properties]\n",
      "    [!docs]?\n",
      "    [!adrs]?\n",
      "    [!identifiers]?\n",
      "    [!impliedRelationships]?\n",
      "    model {\n",
      "        ...\n",
      "    }\n",
      "    views {\n",
      "        ...\n",
      "    }\n",
      "    [configuration]?\n",
      "}\n",
      "```\n",
      "\n",
      "This `workspace` keyword is the top-level language construct in Structurizr DSL, wrapping the `model` and `views`. It can optionally be given a name and description. Additionally, it supports various options like documentations, architecture decision records (ADRS), identifier scopes, implied relationships, model, views, and configuration.\n",
      "\n",
      "CPU times: user 165 ms, sys: 19 ms, total: 184 ms\n",
      "Wall time: 1min 4s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fc09d93c-7e79-424d-a4bd-578a37c5c26c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The provided keyword is `softwareSystem`. Here's its precise grammar according to the rules you've given:\n",
      "\n",
      "```\n",
      "softwareSystem <name> [description] [tags] {\n",
      "    [!docs]\n",
      "    [!adrs]\n",
      "    [group]\n",
      "    [container]\n",
      "    [description]\n",
      "    [tags]\n",
      "    [url]\n",
      "    [properties]\n",
      "    [perspectives]\n",
      "    [-> (relationship)]\n",
      "}\n",
      "```\n",
      "\n",
      "This grammar represents a softwareSystem construct in Structurizr DSL. It has an optional name, description, and tags, as well as several permitted children including `group`, `container`, `description`, `tags`, `url`, `properties`, `perspectives`, and `-> (relationship)`.\n",
      "\n",
      "CPU times: user 139 ms, sys: 16.6 ms, total: 155 ms\n",
      "Wall time: 1min 1s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"softwareSystem\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "02aa5b55-c29c-4a9c-a40a-10484637f432",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The keyword \"workspace\" is used as the top level language construct in Structurizr DSL for defining and configuring a modeling and documentation project. It can optionally be given a name and description, and it can extend another workspace to add more elements, relationships, views, etc. A workspace must contain a model block where elements and relationships are defined.\n",
      "\n",
      "Grammar:\n",
      "```\n",
      "workspace [name] [description] {\n",
      "    ...\n",
      "}\n",
      "\n",
      "permitted children:\n",
      "- name <name>\n",
      "- description <description>\n",
      "- [properties](#properties)\n",
      "- [!docs](#documentation)\n",
      "- [!adrs](#architecture-decision-records-adrs)\n",
      "- [!identifiers](#identifier-scope)\n",
      "- [model](#model)\n",
      "- [views](#views)\n",
      "- [configuration](#configuration)\n",
      "```\n",
      "\n",
      "CPU times: user 178 ms, sys: 36.3 ms, total: 214 ms\n",
      "Wall time: 1min 17s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0f9fe575-3bd5-476b-90d7-efca06daae3e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The `workspace` keyword is the top level language construct in Structurizr DSL, and the wrapper for the model and views. It can optionally be given a name and description. Here's the grammar definition:\n",
      "\n",
      "```\n",
      "workspace [name] [description] {\n",
      "    ...\n",
      "}\n",
      "```\n",
      "\n",
      "Permitted children:\n",
      "- `name <name>`\n",
      "- `description <description>`\n",
      "- `[properties]`\n",
      "- `[!docs]`\n",
      "- `[!adrs]`\n",
      "- `[!identifiers]`\n",
      "- `[!impliedRelationships]`\n",
      "- `model`\n",
      "- `views`\n",
      "- `configuration`\n",
      "\n",
      "CPU times: user 167 ms, sys: 27.3 ms, total: 194 ms\n",
      "Wall time: 1min 10s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "32d27a51-9ccb-4d1e-9f56-389bf62798af",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from langchain_community.vectorstores import DocArrayInMemorySearch\n",
    "\n",
    "# vectorstore = DocArrayInMemorySearch.from_documents(docs2, embeddings)\n",
    "vectorstore = DocArrayInMemorySearch.from_texts([\"sample\"], embeddings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "37a0b6a6-e629-48b5-99f3-474161ec326d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The `extends` keyword in Structurizr DSL is used to extend one workspace with another. It does not have any permitted children and its usage is as follows:\n",
      "\n",
      "```\n",
      "workspace extends <file|url> {\n",
      "    ...\n",
      "}\n",
      "```\n",
      "\n",
      "CPU times: user 69.2 ms, sys: 10.8 ms, total: 80 ms\n",
      "Wall time: 31.7 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"extends\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bb5548c7-df67-45df-b25c-0965337d8cec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The given keyword is \"workspace\". Here's its grammar according to the provided guidelines:\n",
      "\n",
      "```\n",
      "workspace [name] [description] {\n",
      "    [...]\n",
      "}\n",
      "```\n",
      "\n",
      "Permitted children:\n",
      "- name <name>\n",
      "- description <description>\n",
      "- [properties](#properties)\n",
      "- [!docs](#documentation)\n",
      "- [!adrs](#architecture-decision-records-adrs)\n",
      "- [!identifiers](#identifier-scope)\n",
      "- [model](#model)\n",
      "- [views](#views)\n",
      "- [configuration](#configuration)\n",
      "\n",
      "The `workspace` keyword is the top level language construct, serving as a wrapper for the model and views. It can optionally be given a name and description.\n",
      "\n",
      "CPU times: user 193 ms, sys: 20.9 ms, total: 214 ms\n",
      "Wall time: 3min 34s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19a3df8d-aa13-4065-91f6-7a3f2badd594",
   "metadata": {},
   "source": [
    "### Responses before adding \"container\" to modelfile\n",
    "\n",
    "Wrong output :\n",
    "Permitted children:\n",
    "- [deploymentEnvironment](#deploymentEnvironment)\n",
    "- [softwareSystem](#softwareSystem) { ... } // child software systems"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c4816d4c-e760-43ad-a004-2b7657c7d370",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The `container` keyword defines a container, which is a specialized type of software system that can host other software systems.\n",
      "\n",
      "```\n",
      "container <name> [description] [tags] {\n",
      "    ...\n",
      "}\n",
      "```\n",
      "\n",
      "Permitted children:\n",
      "\n",
      "- [!docs](#documentation)\n",
      "- [!adrs](#architecture-decision-records-adrs)\n",
      "- [group](#group)\n",
      "- [deploymentEnvironment](#deploymentEnvironment)\n",
      "- [description](#description)\n",
      "- [tags](#tags)\n",
      "- [url](#url)\n",
      "- [properties](#properties)\n",
      "- [perspectives](#perspectives)\n",
      "- [-> (relationship)](#relationship)\n",
      "- [softwareSystem](#softwareSystem) { ... } // child software systems\n",
      "\n",
      "Note that the `container` keyword has an additional permitted child, which is the definition of one or more contained `softwareSystem` instances.\n",
      "\n",
      "CPU times: user 177 ms, sys: 17.8 ms, total: 195 ms\n",
      "Wall time: 1min 19s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"container\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6dee522d-8cbe-4091-94de-4aa462941900",
   "metadata": {},
   "source": [
    "### Responses before adding \"component\" to modelfile\n",
    "\n",
    "Wrong output :\n",
    "Permitted children:\n",
    "- [container](#container)\n",
    "- [components](#components)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d789ca0f-d496-4126-b39d-bbf97c8a81ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The `component` keyword defines a software component, which is a part of a larger software system. Components can contain other components or elements, and they can have relationships with other components or elements.\n",
      "\n",
      "```\n",
      "component <name> [description] [tags] {\n",
      "    ...\n",
      "}\n",
      "```\n",
      "\n",
      "Permitted children:\n",
      "\n",
      "- [!docs](#documentation)\n",
      "- [!adrs](#architecture-decision-records-adrs)\n",
      "- [group](#group)\n",
      "- [container](#container)\n",
      "- [description](#description)\n",
      "- [tags](#tags)\n",
      "- [url](#url)\n",
      "- [properties](#properties)\n",
      "- [perspectives](#perspectives)\n",
      "- [-> (relationship)](#relationship)\n",
      "- [element](#element)\n",
      "- [components](#components)\n",
      "\n",
      "The `components` child element allows defining nested components within a component.\n",
      "\n",
      "CPU times: user 160 ms, sys: 27.3 ms, total: 187 ms\n",
      "Wall time: 1min 17s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"component\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ec09ca3-fa8c-4079-bd79-587f5d5fdcee",
   "metadata": {},
   "source": [
    "### Responses after adding \"container\" to modelfile\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "2f20c306-91c5-4fc4-832f-fc50e63bbcac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " version:0.0.2\n",
      "\n",
      "```csss-dsl, structurizr\n",
      "container \"Container Name\" [Description] [tags] {\n",
      "  // permitted children for container\n",
      "}\n",
      "```\n",
      "\n",
      "Children:\n",
      "\n",
      "- `description <Description>`\n",
      "- `tags <tags>`\n",
      "- `group <Group>` (optional)\n",
      "- `component <Component>` (optional, repeated)\n",
      "- `properties []` (optional)\n",
      "- `perspectives []` (optional)\n",
      "- `-> (relationship)` (optional, repeated)\n",
      "\n",
      "Children's permitted children:\n",
      "\n",
      "- `group { ... }`\n",
      "  - Permitted children: same as container\n",
      "- `component { ... }`\n",
      "  - Permitted children: same as component\n",
      "- `properties {}`\n",
      "  - No children\n",
      "- `perspectives []`\n",
      "  - `name <Name> [Description]` (optional, repeated)\n",
      "    - Permitted children: none\n",
      "- `-> (relationship)`\n",
      "  - Permitted children:\n",
      "      * `person <PersonName>`\n",
      "      * `softwareSystem <SoftwareSystemName>`\n",
      "      * `container <ContainerName>` (recursive)\n",
      "      * `component <ComponentName>` (recursive)\n",
      "\n",
      "CPU times: user 256 ms, sys: 51.3 ms, total: 307 ms\n",
      "Wall time: 4min 37s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"container\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a53eed23-fb14-4ce3-aac0-e0f4776db5cd",
   "metadata": {},
   "source": [
    "### Responses after adding \"component\" to modelfile\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "86d3a422-12bf-4b51-b569-180c5058695f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The `component` keyword defines a component, within a container.\n",
      "\n",
      "```\n",
      "component <name> [description] [technology] [tags] {\n",
      "    ...\n",
      "}\n",
      "```\n",
      "\n",
      "Permitted children:\n",
      "\n",
      "- [!docs](#documentation)\n",
      "- [!adrs](#architecture-decision-records-adrs)\n",
      "- [description](#description)\n",
      "- [technology](#technology)\n",
      "- [tags](#tags)\n",
      "- [url](#url)\n",
      "- [properties](#properties)\n",
      "- [perspectives](#perspectives)\n",
      "- [-> (relationship)](#relationship)\n",
      "\n",
      "CPU times: user 127 ms, sys: 26.3 ms, total: 153 ms\n",
      "Wall time: 1min 36s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"component\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6f3b92c1-e399-4408-b7b0-7d346edcf883",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The keyword is \"workspace\". Here's the grammar for it based on the provided description:\n",
      "\n",
      "```\n",
      "workspace [name] [description] {\n",
      "    [name] <name>\n",
      "    [description] <description>\n",
      "    [properties]\n",
      "    [!docs]\n",
      "    [!adrs]\n",
      "    [!identifiers]\n",
      "    [!impliedRelationships]\n",
      "    [model]\n",
      "    [views]\n",
      "    [configuration]\n",
      "    extends <file|url>\n",
      "}\n",
      "```\n",
      "\n",
      "CPU times: user 136 ms, sys: 20.3 ms, total: 156 ms\n",
      "Wall time: 3min 49s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2999f843-e446-4f19-ba62-8fd449ba4af4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ```vbnet\n",
      "container {\n",
      "    <name> [description] [technology] [tags] {\n",
      "        ...\n",
      "    }\n",
      "}\n",
      "```\n",
      "\n",
      "version:1.0.0\n",
      "\n",
      "CPU times: user 72.9 ms, sys: 12 ms, total: 84.8 ms\n",
      "Wall time: 3min 38s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"container\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b5410ed-28d0-4dfd-8c7d-5c50bcefd791",
   "metadata": {},
   "source": [
    "## Change modelfile without rebuild model\n",
    "\n",
    "version:1.0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d49c4792-9cb8-4ece-9c17-88ac1f4f87ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ```vbnet\n",
      "{keyword} workspace\n",
      "\n",
      "version:1.0.0\n",
      "\n",
      "workspace [\n",
      "  name \"MyWorkspace\"\n",
      "  description \"This is a description of my workspace\"\n",
      "  {\n",
      "    model {\n",
      "      // Define elements and relationships here\n",
      "    }\n",
      "    views {\n",
      "      // Define views here\n",
      "    }\n",
      "    // Other optional children can be added here\n",
      "  }\n",
      "]\n",
      "```\n",
      "\n",
      "CPU times: user 89.7 ms, sys: 15.2 ms, total: 105 ms\n",
      "Wall time: 33.1 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "693b2b1a-4074-470b-962d-c79977579688",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ```\n",
      "workspace [name] [description] {\n",
      "    // Optional children go here\n",
      "}\n",
      "```\n",
      "\n",
      "The `workspace` keyword defines the root construct of a Structurizr model. It can optionally be given a name and description, and may contain various optional children such as `properties`, `documentation`, `architectureDecisionRecords-adrs`, `identifierScope`, `model`, `views`, `configuration`, or another workspace to extend.\n",
      "\n",
      "Version:1.0.1\n",
      "\n",
      "CPU times: user 89.7 ms, sys: 13.8 ms, total: 104 ms\n",
      "Wall time: 35.3 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "62c24738-318a-4f69-b7b2-15d9c7930270",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ```vbnet\n",
      "{keyword}:\n",
      "workspace [name] [description] {\n",
      "    // Permitted children for workspace keyword go here\n",
      "}\n",
      "```\n",
      "\n",
      "This is the base structure for any keyword in Structurizr DSL. The `{keyword}` placeholder represents where the specific keyword definition will be added. The rest of the code defines an empty workspace with the optional `name` and `description` properties.\n",
      "\n",
      "The permitted children for a custom keyword would go inside the curly braces after the closing semicolon (;) on the last line of this template. These children would depend on the functionality and purpose of your custom keyword.\n",
      "\n",
      "CPU times: user 104 ms, sys: 20.8 ms, total: 125 ms\n",
      "Wall time: 45.3 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "deef58cd-3151-4d65-a275-c2b3fac61932",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ```\n",
      "workspace [name] [description] {\n",
      "    // permitted children go here\n",
      "}\n",
      "```\n",
      "\n",
      "Version:1.0.1\n",
      "\n",
      "CPU times: user 38.1 ms, sys: 6.67 ms, total: 44.7 ms\n",
      "Wall time: 11.8 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9d19db19-7c04-4db9-b4a2-e7e8a8f84646",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The `group` keyword defines a grouping element in a software architecture model. It can be used to organize related elements or relationships.\n",
      "\n",
      "```\n",
      "group <name> [description] {\n",
      "    ...\n",
      "}\n",
      "\n",
      "Permitted children:\n",
      "- description <description>\n",
      "- [elements](#element)\n",
      "- [relationships](#relationship)\n",
      "```\n",
      "\n",
      "The `relationship` keyword defines a relationship between two elements in a software architecture model. It can be used to show the connections and interactions between different components, containers, or software systems.\n",
      "\n",
      "```\n",
      "-> <source> <target> [name] [description] {\n",
      "    ...\n",
      "}\n",
      "\n",
      "Permitted children:\n",
      "- source <elementReference>\n",
      "- target <elementReference>\n",
      "- name <name>\n",
      "- description <description>\n",
      "```\n",
      "\n",
      "In summary, the `group` keyword has children that are elements and relationships, while the `->` relationship keyword has children that are references to its source and target elements.\n",
      "\n",
      "CPU times: user 164 ms, sys: 19.5 ms, total: 183 ms\n",
      "Wall time: 1min 14s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"provide the actual permitted children and generate again\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "7569315e-d38a-4f6c-89b4-11b3caa55865",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ```vbnet\n",
      "keyword: workspace\n",
      "\n",
      "workspace [name] [description] {\n",
      "    // optional children\n",
      "    [properties]\n",
      "    [!docs]\n",
      "    [!adrs]\n",
      "    [!identifiers]\n",
      "    [!impliedRelationships]\n",
      "    model {\n",
      "        // required child\n",
      "        ...\n",
      "    }\n",
      "    views {\n",
      "        // required child\n",
      "        ...\n",
      "    }\n",
      "    [configuration]\n",
      "}\n",
      "\n",
      "// permitted children\n",
      "[name] <name>\n",
      "[description] <description>\n",
      "[properties] {\n",
      "    // optional children\n",
      "    //...\n",
      "}\n",
      "[!docs] {}\n",
      "[!adrs] {}\n",
      "[!identifiers] {}\n",
      "[!impliedRelationships] {}\n",
      "model {\n",
      "    // required child, see the model keyword definition for permitted children\n",
      "    ...\n",
      "}\n",
      "views {\n",
      "    // required child, see the views keyword definition for permitted children\n",
      "    ...\n",
      "}\n",
      "[configuration] {}\n",
      "```\n",
      "version:1.0.3\n",
      "\n",
      "CPU times: user 198 ms, sys: 24.2 ms, total: 223 ms\n",
      "Wall time: 4min 15s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "630b3d22-3146-45e4-bd0b-0f7493000411",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ```\n",
      "keyword: workspace\n",
      "\n",
      "workspace [name] [description] {\n",
      "    [properties]?\n",
      "    [!docs]?\n",
      "    [!adrs]?\n",
      "    [!identifiers]?\n",
      "    [!impliedRelationships]?\n",
      "    model {\n",
      "        ...\n",
      "    }\n",
      "    views {\n",
      "        ...\n",
      "    }\n",
      "    [configuration]?\n",
      "}\n",
      "\n",
      " Permitted children:\n",
      "- name <name>\n",
      "- description <description>\n",
      "- [properties](#properties)\n",
      "- [!docs](#documentation)\n",
      "- [!adrs](#architecture-decision-records-adrs)\n",
      "- [!identifiers](#identifier-scope)\n",
      "- [!impliedRelationships](#impliedrelationships)\n",
      "- model {\n",
      "    ...\n",
      "}\n",
      "- views {\n",
      "    ...\n",
      "}\n",
      "- [configuration](#configuration)\n",
      "```\n",
      "\n",
      "version:1.0.4\n",
      "\n",
      "CPU times: user 161 ms, sys: 23.1 ms, total: 184 ms\n",
      "Wall time: 1min 11s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "cfe7c51f-e5fe-42d6-be10-a74e3a1d4fa4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " The `workspace` keyword is the top level language construct in Structurizr DSL used to define the root node of a software architecture model and documentation. It can optionally be given a name and description, and it contains various elements such as `model`, `views`, `properties`, `documentation`, `architecture-decision-records-adrs`, `identifier-scope`, `impliedrelationships`, and `configuration`. Here's the grammar for the `workspace` keyword:\n",
      "\n",
      "```\n",
      "workspace [name] [description] {\n",
      "    [properties]\n",
      "    [!docs]\n",
      "    [!adrs]\n",
      "    [!identifiers]\n",
      "    [!impliedRelationships]\n",
      "    model {\n",
      "        ...\n",
      "    }\n",
      "    views {\n",
      "        ...\n",
      "    }\n",
      "    [configuration]\n",
      "}\n",
      "```\n",
      "\n",
      "Permitted children:\n",
      "\n",
      "- name <name>\n",
      "- description <description>\n",
      "- properties (optional)\n",
      "- !docs (optional documentation)\n",
      "- !adrs (optional architecture decision records)\n",
      "- !identifiers (optional identifier scope)\n",
      "- !impliedRelationships (optional implied relationships)\n",
      "- model (required)\n",
      "- views (required)\n",
      "- configuration (optional)\n",
      "\n",
      "CPU times: user 231 ms, sys: 34.1 ms, total: 265 ms\n",
      "Wall time: 4min 38s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c15b4551-be24-481b-9922-b5dc57f95e4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ```vbnet\n",
      "{keyword} = version:1.0.5 workspace {\n",
      "    name \"MyWorkspace\"\n",
      "    description \"A sample workspace for Structurizr modeling\"\n",
      "    model {\n",
      "        # Elements and relationships go here\n",
      "    }\n",
      "}\n",
      "```\n",
      "\n",
      "CPU times: user 60.8 ms, sys: 12.6 ms, total: 73.4 ms\n",
      "Wall time: 21.2 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "7ad71c24-154d-4f0a-a96b-6bb712affa19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " version:0.0.1\n",
      "\n",
      "workspace {\n",
      " // Provide a name and description for the workspace if desired\n",
      " // e.g., workspace \"My Software Architecture\" \"A high-level description of this architecture\" {\n",
      " // ...\n",
      " // }\n",
      "\n",
      " model {\n",
      " // Define software systems, containers, components, etc. here\n",
      " // e.g., softwareSystem \"Application Server\" \"An application server software system\" {\n",
      " // ...\n",
      " // }\n",
      " // Continue defining elements and relationships as needed\n",
      " // }\n",
      "}\n",
      "\n",
      "CPU times: user 121 ms, sys: 17.4 ms, total: 138 ms\n",
      "Wall time: 3min 38s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "cb891cb1-1d9c-4577-99d3-bbf9d72830b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " version:0.0.3\n",
      "\n",
      "workspace [name] [description] {\n",
      " <- optional: name >\n",
      " <- optional: description >\n",
      " <- optional: properties >\n",
      " <- optional: !docs >\n",
      " <- optional: !adrs >\n",
      " <- optional: !identifiers >\n",
      " <- optional: !impliedRelationships >\n",
      " model {\n",
      " <- required: group | person | softwareSystem | deploymentEnvironment | element >\n",
      " <- repeated: group | person | softwareSystem | deploymentEnvironment | element >\n",
      " <- repeated: -> (relationship) >\n",
      " }\n",
      " <- optional: views >\n",
      " <- optional: configuration >\n",
      "}\n",
      "\n",
      "CPU times: user 125 ms, sys: 9.85 ms, total: 135 ms\n",
      "Wall time: 55.5 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "6fdf2dfd-de87-4611-b4c8-bf2bec638b07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " version:0.0.3\n",
      "\n",
      "```css\n",
      "container <name> [description] [technology] [tags] {\n",
      "  // permitted children\n",
      "}\n",
      "\n",
      "container <name> [description] [technology] [tags] {\n",
      "  group <groupName> {\n",
      "    // permitted children of group\n",
      "  }\n",
      "  deploymentEnvironment <environmentName> {\n",
      "    // permitted children of deploymentEnvironment\n",
      "  }\n",
      "  component <componentName> {\n",
      "    // permitted children of component\n",
      "  }\n",
      "  element <elementName> {\n",
      "    // permitted children of element\n",
      "  }\n",
      "  <- relationshipName : softwareSystem <relatedSoftwareSystemName> {\n",
      "    // permitted children of relationship\n",
      "  }\n",
      "}\n",
      "```\n",
      "\n",
      "CPU times: user 128 ms, sys: 13.9 ms, total: 142 ms\n",
      "Wall time: 1min 2s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"container\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "4793cf20-8b54-4020-8973-dec06b617eee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ```css\n",
      "workspace [name] [description] {\n",
      "    model {\n",
      "        // model statements here\n",
      "    }\n",
      "    // optional children here\n",
      "}\n",
      "\n",
      "model {\n",
      "    // model statements here\n",
      "    group {\n",
      "        // group statements here\n",
      "    }\n",
      "    person <name> [description] [tags] {\n",
      "        // person statements here\n",
      "    }\n",
      "    softwareSystem <name> [description] [tags] {\n",
      "        container <name> [description] [technology] [tags] {\n",
      "            component <name> [description] [technology] [tags] {\n",
      "                // component statements here\n",
      "            }\n",
      "            // optional children here\n",
      "        }\n",
      "        // optional children here\n",
      "    }\n",
      "    deploymentEnvironment <name> {\n",
      "        // deployment environment statements here\n",
      "    }\n",
      "    // optional children here\n",
      "}\n",
      "```\n",
      "\n",
      "CPU times: user 181 ms, sys: 26.7 ms, total: 208 ms\n",
      "Wall time: 4min 14s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for chunk in chain.stream(\"workspace\"):\n",
    "    print(chunk, end=\"\", flush=True)\n",
    "\n",
    "print(\"\\n\", flush=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a49aa6a2-6ecd-4fdb-b049-409b43c9ab31",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
